#include "stm32f10x.h"                  // Device header
//using A2/A3 as the EXTI_PORT
int32_t Encoder_Num=0;
void Encoder_Init()
{

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource3);
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStructure);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure2;
	NVIC_InitStructure2.NVIC_IRQChannel=EXTI2_IRQn;   
	NVIC_InitStructure2.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructure2.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStructure2);
	NVIC_InitTypeDef NVIC_InitStructure3;
	NVIC_InitStructure3.NVIC_IRQChannel=EXTI3_IRQn;   
	NVIC_InitStructure3.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructure3.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_InitStructure3);

	}

	
	void EXTI2_IRQHandler(void)
	{
		if(EXTI_GetITStatus(EXTI_Line2)==SET)
		{
			if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)==0)
				Encoder_Num++;
			EXTI_ClearITPendingBit(EXTI_Line2);
		}	
		
			
	}
	
		void EXTI3_IRQHandler(void)
	{
		
		if(EXTI_GetITStatus(EXTI_Line3)==SET)
		{
			if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)==0)
					Encoder_Num--;
			EXTI_ClearITPendingBit(EXTI_Line3);
		}	
	}
	
int32_t Encoder_Get(void)
{

	return Encoder_Num;
}
